package com.ny.controller;

import com.ny.entity.Device;
import com.ny.entity.Result;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    // 查询所有设备信息：http://127.0.0.1:8081/findAll
    @GetMapping("/findAll")
    public Result findAll() {
        return Result.success(deviceService.findAll());}

    // 根据设备名称查询设备信息: http://127.0.0.1:8081/findByDeviceName?deviceName=卧室空调
    @GetMapping("/findByDeviceName")
    public Result findByDeviceName(@RequestParam String deviceName) {
        return Result.success(deviceService.findByDeviceName(deviceName));
    }
    
    // 根据时间范围和状态查询设备（离线设备查询）
    @GetMapping("/findOfflineByTimeRange")
    public Result findOfflineByTimeRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {

        List<Device> devices = deviceService.findOfflineByTimeRange(startTime, endTime);
        return Result.success(devices);
    }

}
